from PIL import Image
import paddle
from paddleocr import PaddleOCR, draw_ocr
import requests
import cv2
import numpy as np
import functools
import json

img_path = "1.jpg"

# 左上角开始 顺时针

paddle.utils.run_check()

img = cv2.imread(img_path)

point1 = (150, 600)
point2 = (1050, 2170)

ocr = PaddleOCR(use_angle_cls=False, lang="ch", use_gpu=True)


def custom_cmp(x, y):
    p1 = x[0][0]
    p2 = y[0][0]
    if abs(p1[1]-p2[1]) > 70:
        return (p1[1]-p2[1])*1000
    else:
        return p1[0]-p2[0]


dx = int((point2[0]-point1[0])/8)
dy = int((point2[1]-point1[1])/14)
ss = 0
s = []
for i in range(160):
    x = point1[0]+(i % 10-1)*dx
    y = point1[1] + (i//10-1) * dy
    img_line = img[y:y+dy, x:x+dx]
    result = ocr.ocr(img_line, cls=True)[0][0]
    s.append(result[1][0])

rr = []
for y in range(16):
    d = []
    for x in range(10):
        d.append(int(s[x+y*10]))
    rr.append(d)

with open("result.json", "w") as f:
    f.write(json.dumps(rr))
    pass
print(rr)
